package 链表.删除链表中重复元素;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2022/4/13 20:25
 * @description:给定一个已排序的链表的头 head ， 删除所有重复的元素，使每个元素只出现一次 。返回 已排序的链表 。
 * @version:
 */
public class Solution {
    public static void main(String[] args) {
        ListNode root = new ListNode();//头节点
        root.next = new ListNode(1);
        root.next.next = new ListNode(1);
        root.next.next.next = new ListNode(2,new ListNode(3,new ListNode(3)));
        ListNode listNode = test(root.next);
        System.out.println(listNode.toString());
    }

    public static ListNode test(ListNode head) {
        ListNode slow = head;
        if (slow == null || slow.next == null) {
            return slow;
        }
        while (slow != null) {
            ListNode fast = slow.next;
            while (fast != null && fast.val == slow.val) {
                fast = fast.next;
            }
            slow.next = fast;
            slow = fast;
        }
        return head;
    }




}
